package lab03.test06;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

//map   key - value
//      单词  次数
public class Test {
    public static void main(String[] args) {
        String path = "E:\\words.txt";//找到文件，打开它
        RandomAccessFile file =null;
        try {
             file = new RandomAccessFile(path,"rw");

            String line = null;
            Map<String, Integer> map = new HashMap<>();
            try {
                while ((line=file.readLine())!=null){
                    System.out.println(line);
                   //System.out.println(value);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            //先要判断map中有没有这个key，没有直接将value赋值给1
            //如果有就在之前的value上加一个1（相当于更新value的值）
            int value = 0;
            if (map.get(line) == null){
                map.put(line,1);
                System.out.println(value);
            }else{
                value = map.get(line);
                value = value+1;
                map.put(line,value);
                System.out.println(value);
                //map.put(line,map.get(line) + 1);一行可以解决
            }

            try {
                file.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
        try {
         file.close();
           } catch (IOException e) {
              e.printStackTrace();
         }
        }
    }
}
